package jsu.blogger.service.impl;

import jsu.blogger.filter.DataFilter;
import jsu.blogger.mapper.*;
import jsu.blogger.pojo.*;
import jsu.blogger.service.inter.GetUserDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Service
public class GetUserDataServiceImpl implements GetUserDataService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UserStatsMapper userStatsMapper;

    @Autowired
    private UserFocusMapper userFocusMapper;

    @Autowired
    private UserFoldersMapper userFoldersMapper;

    @Autowired
    private TagsMapper tagsMapper;

    @Autowired
    private UserTagsMapper userTagsMapper;

    @Autowired
    private PostMapper postMapper;

    @Value("${dir.userAvatarDir}")
    private  String userAvatarDir;
    private static final Logger log = LoggerFactory.getLogger(DataFilter.class);
    @Override
    public String getUsernameByUserId(Long UserId){
        String username=null;
        Users user=usersMapper.selectUserByUId(UserId);
        if(user!=null){
            username=user.getUsername();
        }
        return username;
    }

    @Override
    public UserStats getOtherUserDataByUserId(Long userId){
        return userStatsMapper.selectUserStatsByUID(userId);
    }

    @Override
    public File getUserAvatar(Long userId) {
        String userAvatarFileNamePrefix = String.valueOf(userId);
        Path userAvatarDirectory = Paths.get(userAvatarDir);

        // 确保目录存在
        if (!Files.exists(userAvatarDirectory) || !Files.isDirectory(userAvatarDirectory)) {
            return null; // 目录不存在，返回 null
        }

        // 使用 DirectoryStream 遍历文件
        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(userAvatarDirectory,
                path -> path.getFileName().toString().startsWith(userAvatarFileNamePrefix))) {
            for (Path path : directoryStream) {
                return path.toFile(); // 找到第一个匹配的文件，立即返回
            }
        } catch (IOException e) {
            log.error(String.valueOf(e));
        }

        return null; // 没有找到匹配的文件
    }

    @Override
    public List<Users> getUserFollowingList(Long userId, int currentPage, int pageSize) {
        List<Users> resultList = new ArrayList<>();
        int begin=currentPage * pageSize;
        List<Long> userIdList=userFocusMapper.selectFollowedIdPageByFollowerId(userId,begin,pageSize);
        for (Long id:
             userIdList) {
            if(id!=null){
                String username=usersMapper.selectUsernameByUserId(id);
                resultList.add(new Users(id,username));
            }
        }
        return resultList;
    }
    @Override
    public List<Users> getUserFollowingListYxp(Long userId) {
        List<Users> resultList = new ArrayList<>();
        List<Long> userIdList=userFocusMapper.selectFollowedIdByFollowerId(userId);
        for (Long id:
                userIdList) {
            if(id!=null){
                String username=usersMapper.selectUsernameByUserId(id);
                resultList.add(new Users(id,username));
            }
        }
        return resultList;
    }

    @Override
    public List<UserFolder> getUserFolderList(Long userId) {
        return userFoldersMapper.selectFolderListByUserId(userId);
    }

    @Override
    public List<UserFolder> getUserFolderList(Long userId, int visibility) {
        return null;
    }

    @Override
    public List<Tag> getUserInterestTagList(Long userId) {
        List<Long> tagIdList=userTagsMapper.selectTagIdListByUserId(userId);
        List<Tag> tagList=new ArrayList<>();
        for (Long tagId:tagIdList) {
            String tagName=tagsMapper.selectTagNameByTagId(tagId);
            if(tagName!=null){
                tagList.add(new Tag(tagId,tagName));
            }
        }
        return tagList;
    }

    @Override
    public List<Post> getPostsPageByUserId(Long userId, int currentPage, int pageSize) {
        List<Post> posts=postMapper.selectPoseListPageByUserId(userId,currentPage,pageSize,0);
        return posts;
    }


    public List<Tag> getInterestTags(){
        List<Tag> tags=tagsMapper.selectAllTags();
        //System.out.println(tags);
        log.info("getInterestTags");
        return tags;
    }

    @Override
    public String getEmailByUserId(Long UserId){

        return userStatsMapper.getEmail(UserId);
    }
}
